package com.vshop.platform.system.crud.service;

import cn.hutool.core.util.ObjectUtil;
import com.mybatisflex.core.query.QueryWrapper;

import com.vshop.framework.core.exception.ServiceException;
import com.vshop.framework.mybatisflex.datapermission.DataPermHelper;
import com.vshop.framework.satoken.helper.LoginHelper;
import com.vshop.platform.system.crud.dao.SysUserDao;

import com.vshop.platform.system.domain.entity.SysDept;
import com.vshop.platform.system.domain.entity.SysRole;
import com.vshop.platform.system.domain.entity.SysUser;
import com.vshop.platform.system.domain.param.SysDeptParam;
import com.vshop.platform.system.domain.param.SysRoleParam;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import static com.vshop.platform.system.domain.entity.table.SysRoleTableDef.SysRoleTable;
import static com.vshop.platform.system.domain.entity.table.SysUserTableDef.SysUserTable;


/**
 * 通用"数据权限过滤“服务
 */
@RequiredArgsConstructor
@Service
public class DataScopeService {

    private final SysUserDao baseDao;
//    private final SysRoleRepository roleRepository;
//    private final SysDeptRepository deptRepository;


    /**
     * 校验用户是否有数据权限
     *
     * @param userId 用户id
     */
    public void checkUserDataScope(Long userId) {
        if (ObjectUtil.isNull(userId)) {
            return;
        }
        if (LoginHelper.isSuperAdmin()) {
            return;
        }
        QueryWrapper queryWrapper = QueryWrapper.create()
                .from(SysUserTable)
                .where(SysUserTable.UserId.eq(userId));
        queryWrapper = DataPermHelper.buildDataScope(queryWrapper);
        SysUser entity = baseDao.getOne(queryWrapper);
        if (ObjectUtil.isEmpty(entity)) {
            throw new ServiceException("没有权限访问用户数据！");
        }
    }

    /**
     * 校验部门是否有数据权限
     *
     * @param deptId 部门id
     */
    public void checkDeptDataScope(Long deptId) {
        if (ObjectUtil.isNull(deptId)) {
            return;
        }
        if (LoginHelper.isSuperAdmin()) {
            return;
        }

        SysDeptParam dept = new SysDeptParam();
        dept.setDeptId(deptId);
        QueryWrapper queryWrapper = QueryWrapper.create()
                .from(SysUserTable)
                .where(SysUserTable.DeptId.eq(deptId));
        queryWrapper = DataPermHelper.buildDataScope(queryWrapper);
//        SysDept entity = baseDao.getOne(queryWrapper);
        SysDept entity = baseDao.getObjAs(queryWrapper, SysDept.class);
        if (ObjectUtil.isNull(entity)) {
            throw new ServiceException("没有权限访问部门数据！");
        }
    }

    /**
     * 校验角色是否有数据权限
     *
     * @param roleId 角色id
     */
    public void checkRoleDataScope(Long roleId) {
        if (ObjectUtil.isNull(roleId)) {
            return;
        }
        if (LoginHelper.isSuperAdmin()) {
            return;
        }

        SysRoleParam role = new SysRoleParam();
        role.setRoleId(roleId);
        QueryWrapper queryWrapper = QueryWrapper.create()
                .from(SysRoleTable)
                .where(SysRoleTable.RoleId.eq(roleId));
//        SysRole entity = baseDao.getOne(queryWrapper);
        SysRole entity = baseDao.getObjAs(queryWrapper, SysRole.class);
        if (ObjectUtil.isNull(entity)) {
            throw new ServiceException("没有权限访问角色数据！");
        }
    }

}
